/**
 * Created by huangzy on 2015/5/20.
 * http://www.12301.cc/tpl/membercard/js/membercard.register.js
 */

var M = RichBase.extend({
	statics : {
		mode : "", // edit || create  页面处于编辑帐号模式 或 新建帐号模式
		api : {
			//提交表单
			submit : "call/jh_card.php",
			//获取验证码
			getCode : "call/jh_card.php?action=code",
			//检查用户是否注册过
			valiuser : "call/jh_mem.php?action=chkAndGet2",
			modify_user : "call/modify_user.php"
		},
		isMyDistributor : false,  // 0:不是我的分销商   1:是我的分销商
		passwordInpVal : "6-16个字符,请使用字母加数字或符号的组合",
		valifyMobile : function(mobile){
			var reg = /^0?(13[0-9]|15[012356789]|18[012356789]|14[57])[0-9]{8}$/;
			if(!mobile || mobile.length!==11) return false;
			return true;
//			return reg.test(mobile);
		},
		valifyEmail : function(email){
			var reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
			if(!email) return false;
			return reg.test(email);
		}
	},
	EVENTS : {
		"click" : {
			"#famleWrap .fe" : "switchSex",
			"#getCheckmaBtn" : "getMa",
			"#readCheckbox" : "onreadCheckboxClick",
			"#saveBtn" : "formSubmit"
		},
		"keyup" : {
			"#mobileInp" : "onMobileInputKeyup"
		},
		"focus" : {
			".valifyInp" : "onInputFocus"
		},
		"blur" : {
			".valifyInp" : "onInputBlur"
		},
		"change" : {
			"#compTypeSelect" : "onCompTypeSelectChange"
		}
	},
	init : function(opt){
		var that = this;
		this.statics.mode = $("#inpHid_mobile").val() ? "edit" : "create";
		this.mobileInp = $("#mobileInp");
		this.getMaBtn = $("#getCheckmaBtn");
        this.saveBtn = $("#saveBtn");
		this.errorMobile = $("#mobileInpErrorTxt");
		this.mobileExistTxt = $("#mobileExistTxt");
		var hidInp_dtype = $("#inpHid_dtype").val();
		var hidInp_comtype = $("#inpHid_comtype").val();
		this.inpHid_did = $("#inpHid_did");
		this.passwordInp = $("#passwordInp");
		this.rePasswordInp = $("#repasswordInp");
		this.passwordForm = $("#formBox_password");
		this.idcardInp = $("#idcardInp");
		this.dnameInp = $("#dnameInp");
		this.cnameInp = $("#cnameInp");
		this.QQInp = $("#QQInp");
		this.checkmaInp = $("#checkmaInp");
		this.statics.isMyDistributor = $("#inpHid_isMyDistributor").val()==0 ? false : true;
	},
	formSubmit : function(that,e){
		var allow = true;
		var loading = $("#formLoading");
        var saveBtn = that.saveBtn;
		e.preventDefault();
        if(saveBtn.hasClass("disable")) return false;
		$("#registerForm .valifyInp").each(function(){
			var inp = $(this);
			inp.blur();
			if(inp.hasClass("error")) allow = false;
		})
		if(!allow) return false;
		PFT_GLOBAL.G.Ajax({
			url : "call/jh_card.php",
			type : "post",
			data : $("#registerForm").serialize(),
			loading : function(){
				saveBtn.addClass("loading");
			},
			removeLoading : function(){
				saveBtn.removeClass("loading");
			},
			timeout : function(){},
			serverError : function(){}
		},function(res){
			if(res.status == "ok"){//注册成功
				var did = res.body.split("|") || "";
				window.location.href = "http://www.12301.cc/register_card.html?did="+did;
			}else if(res.status=="success"){ //更新成功
				var did = $("#inpHid_did").val();
				window.location.href = "http://www.12301.cc/register_card.html?did="+did;
			}else{
				alert(res.msg);
			}
		})
	},
	//切换性别
	switchSex : function(that,e){
		var btn = $(e.currentTarget);
		btn.parent().prev().val(btn.attr("data-type"));
		btn.addClass("active").siblings().removeClass("active");
	},
	setSex : function(fm){
		if(!fm) return false;
		fm = fm.toUpperCase();
		$("#hidSexInp").val(fm);
		if(fm=="M"){
			$("#famleWrap").children(".M").addClass("active").siblings().removeClass("active");
		}else{
			$("#famleWrap").children(".F").addClass("active").siblings().removeClass("active");
		}
	},
	//获取验证码
	getMa : function(that,e){
		var btn = $(e.currentTarget);
		var inp = $("#mobileInp");
		var mobile = inp.val();
		var error = $("#mobileInpErrorTxt");
		var url = that.statics.api.getCode;
		if(!mobile){
			error.show().find(".t").text("请输入手机号");
			inp.addClass("error");
			return false;
		}
		if(!that.statics.valifyMobile(mobile)){
			error.show().find(".t").text("手机号格式错误");
			inp.addClass("error");
			return false;
		}
		PFT_GLOBAL.G.Ajax({
			url : url,
			data : { mobile:mobile},
			loading : function(){ btn.text("获取中...")},
			removeLoading : function(){ btn.text("获取验证码")},
			timeout : function(){},
			serverError : function(){}
		},function(res){
			if(res.status=="success"){
				PFT_GLOBAL.U.Alert("success",'<p style="width:180px;">验证码已发送至您手机</p>',3*1000);
			}else{
				PFT_GLOBAL.U.Alert("error",'<p style="width:180px;">'+res.msg+'</p>');
			}
		})
	},
	onInputBlur : function(that,e){
		var tarInp = $(e.currentTarget);
		var val = tarInp.val();
		var errorMsg = tarInp.parents(".line").find(".errortip");
		var require = tarInp.attr("require");
		var valMobile = tarInp.attr("mobile");
		var vcode = tarInp.attr("vcode");
		var repassword = tarInp.attr("vcode");
		var error = function(){
			errorMsg.show();
			tarInp.addClass("error");
		};
		//验证必填项
		if(!val && typeof require!=="undefined") error();
		//验证手机号
		if(typeof valMobile!=="undefined" && !that.statics.valifyMobile(val)) error();
		//验证验证码
		if(typeof vcode!=="undefined" && val.length!=6) error();
		//验证密码
		if(tarInp.hasClass("passwordInp")){
			var min = tarInp.attr("data-min");
			var max = tarInp.attr("data-max");
			var len = val.length;
			if(len<min || len>max) error();
		}
		//验证第二次密码
		if(tarInp.hasClass("repasswordInp")){
			var pas = $("#passwordInp").val();
			if(pas && val!=pas) error();
		}

	},
	onRegisted : function(){
		this.passwordInp.removeClass("valifyInp");
		this.rePasswordInp.removeClass("valifyInp");
		this.passwordForm.hide();
	},
	onUnregist : function(){
		this.passwordInp.addClass("valifyInp").val("");
		this.rePasswordInp.addClass("valifyInp").val("");
		this.passwordForm.show();
		this.inpHid_did.val("");
		this.idcardInp.val("");
		this.dnameInp.val("");
		this.cnameInp.val("");
		this.QQInp.val("");
		this.checkmaInp.val("");
		this.setSex("M");
		this.passwordForm.show();
	},
	onMobileInputKeyup : function(that,e){
		var mobile = that.mobileInp.val();
		var url = that.statics.api.valiuser;
		var tip = that.mobileExistTxt;
		if(mobile.length!=11){
			tip.hide();
			that.onUnregist();
		}else if(that.statics.valifyMobile(mobile)){
			//检查该手机号是否被注册过
			PFT_GLOBAL.G.Ajax({
				url : url,
				data : { mobile:mobile},
				loading : function(){},
				removeLoading : function(){},
				timeout : function(){ },
				serverError : function(){}
			},function(res){
				//code: 100存在   101不存在   102散客   103此帐号已被禁用或删除
				var code = res.code;
				var data = res.data;
				var idcardInp = $("#idcardInp");
				if(code==100 || code==102){
					var cname = data["cname"];
					var com_type = data["com_type"];
					var dname = data["dname"];
					var dtype = data["dtype"];
					var id = data["id"];
					var mobile = data["mobile"];
					var passport = data["passport"];
					var qq = data["qq"];
					var status = data["status"];
					var sex = data["sex"] || "M";
					$("#dnameInp").val(dname);
					$("#cnameInp").val(cname);
					$("#QQInp").val(qq);
					$("#repasswordInp").val(passport);
					$("#passwordInp").val(passport);
					that.setSex(sex);
					$("#inpHid_did").val(id);
					$("#formBox_password").hide();
					tip.show().children().html("此帐号已注册");
					if(idcardInp.val()) idcardInp.attr("readonly","");
					that.onRegisted();
				}else if(code==103){
					tip.show().children().html("此帐号已被禁用或删除");
					idcardInp.removeAttr("readonly");
					that.onUnregist();
				}else{
					tip.hide().children().html("");
					idcardInp.removeAttr("readonly");
					that.onUnregist();
				}
			})
		}
	},
	onInputFocus : function(that,e){
		var tarInp = $(e.currentTarget);
		var val = tarInp.val();
		var error = tarInp.parent().find(".errortip");
		tarInp.removeClass("error");
		error.hide();
		if(tarInp.hasClass("shopname")){
			tarInp.parents(".inpBox").removeClass("error").next(".errortip").hide();
		}
		if(tarInp.hasClass("companyname")){
			tarInp.parents(".inpBox").removeClass("error").next(".warmtip").removeClass("error");
		}
		if(tarInp.hasClass("passwordInp")) error.show();
	},
	onCompTypeSelectChange : function(that,e){
		var memberType = $(e.currentTarget).val();
		that.switchCompanynameInp(memberType);
	},
	onreadCheckboxClick : function(that,e){
		var checkbox = $(e.currentTarget);
		if(checkbox.is(":checked")){
			checkbox.parent().find(".errortip").hide();
		}
	},
	switchCompanynameInp : function(membertype){
		if(membertype=="个人"){
			$("#companynameInpContainer").hide();
			$("#companyname").addClass("noexist");
		}else{
			$("#companynameInpContainer").show();
			$("#companyname").removeClass("noexist");
		}
	}
});

$(function(){
	new M({container:$("#memberCardContainer")});
})


